<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="隐式动画非RootLayer上的动画为隐式动画,只是修改Layer的属性自动就会有动画">
<meta property="og:type" content="article">
<meta property="og:title" content="Animation">
<meta property="og:url" content="http://yoursite.com/2018/07/12/Animation/index.html">
<meta property="og:site_name" content="Erhqe">
<meta property="og:description" content="隐式动画非RootLayer上的动画为隐式动画,只是修改Layer的属性自动就会有动画">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-07-19T05:40:06.062Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Animation">
<meta name="twitter:description" content="隐式动画非RootLayer上的动画为隐式动画,只是修改Layer的属性自动就会有动画">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/07/12/Animation/"/>





  <title>Animation | Erhqe</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Erhqe</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/07/12/Animation/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Animation</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-07-12T22:59:59+08:00">
                2018-07-12
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Animation/" itemprop="url" rel="index">
                    <span itemprop="name">Animation</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/07/12/Animation/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2018/07/12/Animation/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h4 id="隐式动画"><a href="#隐式动画" class="headerlink" title="隐式动画"></a>隐式动画</h4><p><strong>非RootLayer上的动画为隐式动画,只是修改Layer的属性自动就会有动画</strong><a id="more"></a></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">layer.backgroundColor = <span class="type">UIColor</span>.red.cgColor</span><br><span class="line">layer.bounds = <span class="type">CGRect</span> (x: <span class="number">0</span>, y: <span class="number">0</span>, width: <span class="number">100</span>, height: <span class="number">100</span>)</span><br><span class="line">layer.anchorPoint = <span class="type">CGPoint</span> (x: <span class="number">0</span>, y: <span class="number">0</span>)	<span class="comment">//	相对本身</span></span><br><span class="line">layer.position = <span class="type">CGPoint</span> (x: <span class="number">0</span>, y: <span class="number">200</span>)		<span class="comment">// 相对父视图 相当于Center</span></span><br><span class="line">layer.delegate = <span class="keyword">self</span></span><br><span class="line">layer.setNeedsDisplay()</span><br><span class="line">view.layer.addSublayer(layer)</span><br></pre></td></tr></table></figure>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">touchesBegan</span><span class="params">(<span class="number">_</span> touches: Set&lt;UITouch&gt;, with event: UIEvent?)</span></span> &#123;</span><br><span class="line">        <span class="comment">//  关闭隐式的动画的两行代码</span></span><br><span class="line">        <span class="comment">//CATransaction.begin()</span></span><br><span class="line">        <span class="comment">//CATransaction.setDisableActions(true)</span></span><br><span class="line">        <span class="comment">//隐式动画</span></span><br><span class="line">        layer.bounds = <span class="type">CGRect</span> (x: <span class="number">0</span>, y: <span class="number">0</span>, width: <span class="number">50</span>, height: <span class="number">50</span>)</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h5 id="Layer绘制"><a href="#Layer绘制" class="headerlink" title="Layer绘制"></a>Layer绘制</h5><p><strong>方法一创建继承自CALayer 重写draw(in ctx: CGContext),手动调用setNeedsDisplay ()</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TestLayer</span>: <span class="title">CALayer</span> </span>&#123;</span><br><span class="line">    <span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">draw</span><span class="params">(<span class="keyword">in</span> ctx: CGContext)</span></span> &#123;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>方法二设置代理,在代理方法中绘制,手动调用setNeedsDisplay ()</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">ViewController</span> : <span class="title">CALayerDelegate</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">draw</span><span class="params">(<span class="number">_</span> layer: CALayer, <span class="keyword">in</span> ctx: CGContext)</span></span> &#123;</span><br><span class="line">        <span class="comment">//  画图</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="CoreAnimation-BasicAnimation"><a href="#CoreAnimation-BasicAnimation" class="headerlink" title="CoreAnimation-BasicAnimation"></a>CoreAnimation-BasicAnimation</h4><p><strong>CoreAnimation用于CALayer</strong></p>
<p><strong>平移KVC  赋值 key写的什么toValue赋对应的值,只能设置三个属性tovalue,fromValue,ByValue弊端</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//	KeyPath 指定动画类型</span></span><br><span class="line"><span class="keyword">let</span> basicAnimation = <span class="type">CABasicAnimation</span> (keyPath: <span class="string">"position.x"</span>)</span><br><span class="line"><span class="comment">//  从哪儿! 不写FromValue的话从当前位置</span></span><br><span class="line">basicAnimation.fromValue = <span class="type">CGPoint</span> (x: <span class="number">0</span>, y: <span class="number">0</span>)</span><br><span class="line"><span class="comment">//  到哪儿</span></span><br><span class="line">basicAnimation.toValue = <span class="type">CGPoint</span> (x: <span class="number">300</span>, y: <span class="number">300</span>)</span><br><span class="line"><span class="comment">//  当前位置的基础上执行</span></span><br><span class="line"><span class="comment">//basicAnimation.byValue =  CGPoint (x: 200, y: 150) </span></span><br><span class="line"><span class="comment">//  设置动画执行完毕之后不删除动画</span></span><br><span class="line">basicAnimation.isRemovedOnCompletion = <span class="literal">false</span>    <span class="comment">//这两行的作用是执行完动画不反弹</span></span><br><span class="line"><span class="comment">//  设置保存动画的最新状态</span></span><br><span class="line">basicAnimation.fillMode = kCAFillModeForwards</span><br><span class="line"><span class="comment">//  动画时长</span></span><br><span class="line">basicAnimation.duration = <span class="number">5</span></span><br><span class="line">layer.add(basicAnimation, forKey: <span class="literal">nil</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> basicAnimation = <span class="type">CABasicAnimation</span> (keyPath: <span class="string">"transform"</span>)</span><br><span class="line"> <span class="comment">//  X 方向偏移 0 Y方向偏移 100 第三个参数随便写</span></span><br><span class="line">basicAnimation.toValue = <span class="type">CATransform3DMakeTranslation</span>(<span class="number">0</span>, <span class="number">100</span>, <span class="number">0</span>)</span><br><span class="line">        </span><br><span class="line"><span class="comment">//  设置动画执行完毕之后不删除动画</span></span><br><span class="line">basicAnimation.isRemovedOnCompletion = <span class="literal">false</span>    <span class="comment">//这两行的作用是执行完动画不反弹</span></span><br><span class="line"><span class="comment">//  设置保存动画的最新状态</span></span><br><span class="line">basicAnimation.fillMode = kCAFillModeForwards</span><br><span class="line">basicAnimation.duration = <span class="number">2</span></span><br><span class="line">        </span><br><span class="line">layer.add(basicAnimation, forKey: <span class="literal">nil</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> basicAnimation = <span class="type">CABasicAnimation</span> (keyPath: <span class="string">"transform.translation.y"</span>)</span><br><span class="line"><span class="comment">//  X 方向偏移 0 Y方向偏移 100 第三个参数随便写</span></span><br><span class="line">basicAnimation.toValue = <span class="number">100</span></span><br><span class="line">        </span><br><span class="line"><span class="comment">//  设置动画执行完毕之后不删除动画</span></span><br><span class="line">basicAnimation.isRemovedOnCompletion = <span class="literal">false</span>    <span class="comment">//这两行的作用是执行完动画不反弹</span></span><br><span class="line"><span class="comment">//  设置保存动画的最新状态</span></span><br><span class="line">basicAnimation.fillMode = kCAFillModeForwards</span><br><span class="line">basicAnimation.duration = <span class="number">2</span></span><br><span class="line">        </span><br><span class="line">layer.add(basicAnimation, forKey: <span class="literal">nil</span>)</span><br></pre></td></tr></table></figure>
<p><strong>缩放</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> basicAnimation = <span class="type">CABasicAnimation</span> (keyPath: <span class="string">"bounds"</span>)	<span class="comment">//transform.scale transform.scale.x</span></span><br><span class="line">basicAnimation.toValue = <span class="type">CGRect</span> (x: <span class="number">0</span>, y: <span class="number">0</span>, width: <span class="number">200</span>, height: <span class="number">300</span>)</span><br><span class="line"><span class="comment">//  设置动画执行完毕之后不删除动画</span></span><br><span class="line">basicAnimation.isRemovedOnCompletion = <span class="literal">false</span>    <span class="comment">//这两行的作用是执行完动画不反弹</span></span><br><span class="line"><span class="comment">//  设置保存动画的最新状态</span></span><br><span class="line">basicAnimation.fillMode = kCAFillModeForwards</span><br><span class="line">basicAnimation.duration = <span class="number">5</span></span><br><span class="line">        </span><br><span class="line">layer.add(basicAnimation, forKey: <span class="literal">nil</span>)</span><br></pre></td></tr></table></figure>
<p><strong>旋转</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> basicAnimation = <span class="type">CABasicAnimation</span> (keyPath: <span class="string">"transform"</span>)</span><br><span class="line">basicAnimation.toValue = <span class="type">CATransform3DMakeRotation</span>(.pi / <span class="number">4</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>)</span><br><span class="line"><span class="comment">//  设置动画执行完毕之后不删除动画</span></span><br><span class="line">basicAnimation.isRemovedOnCompletion = <span class="literal">false</span>    <span class="comment">//这两行的作用是执行完动画不反弹</span></span><br><span class="line"><span class="comment">//  设置保存动画的最新状态</span></span><br><span class="line">basicAnimation.fillMode = kCAFillModeForwards</span><br><span class="line">basicAnimation.duration = <span class="number">2</span></span><br><span class="line">        </span><br><span class="line">layer.add(basicAnimation, forKey: <span class="literal">nil</span>)</span><br></pre></td></tr></table></figure>
<h4 id="CoreAnimation-KeyFrameAnimation"><a href="#CoreAnimation-KeyFrameAnimation" class="headerlink" title="CoreAnimation-KeyFrameAnimation"></a>CoreAnimation-KeyFrameAnimation</h4><p><strong>四方形转一圈</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> keyFrameAnimation = <span class="type">CAKeyframeAnimation</span>(keyPath: <span class="string">"position"</span>)</span><br><span class="line">keyFrameAnimation.values = [<span class="type">CGPoint</span>.zero,<span class="type">CGPoint</span> (x: <span class="number">100</span>, y: <span class="number">0</span>),<span class="type">CGPoint</span> (x: <span class="number">100</span>, y: <span class="number">200</span>),<span class="type">CGPoint</span> (x: <span class="number">0</span>, y: <span class="number">200</span>),<span class="type">CGPoint</span>.zero]</span><br><span class="line">keyFrameAnimation.duration = <span class="number">3</span></span><br><span class="line"><span class="comment">//  设置动画执行完毕之后不删除动画</span></span><br><span class="line">keyFrameAnimation.isRemovedOnCompletion = <span class="literal">false</span>    <span class="comment">//这两行的作用是执行完动画不反弹</span></span><br><span class="line"><span class="comment">//  设置保存动画的最新状态</span></span><br><span class="line">keyFrameAnimation.fillMode = kCAFillModeForwards</span><br><span class="line"><span class="comment">//  动画节奏┭┮﹏┭┮很少用</span></span><br><span class="line">keyFrameAnimation.timingFunction = <span class="type">CAMediaTimingFunction</span> (name: kCAMediaTimingFunctionEaseInEaseOut)</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        kCAMediaTimingFunctionEaseIn 开始慢结尾快</span></span><br><span class="line"><span class="comment">        kCAMediaTimingFunctionLinear  匀速</span></span><br><span class="line"><span class="comment">        kCAMediaTimingFunctionDefault   默认</span></span><br><span class="line"><span class="comment">        kCAMediaTimingFunctionEaseOut 开始快结尾慢</span></span><br><span class="line"><span class="comment">        kCAMediaTimingFunctionEaseInEaseOut 开始和结束慢中间快</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">layer.add(keyFrameAnimation, forKey: <span class="literal">nil</span>)</span><br></pre></td></tr></table></figure>
<p><strong>监听动画状态</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">keyFrameAnimation.delegate = <span class="keyword">self</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">animationDidStart</span><span class="params">(<span class="number">_</span> anim: CAAnimation)</span></span> &#123;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">animationDidStop</span><span class="params">(<span class="number">_</span> anim: CAAnimation, finished flag: Bool)</span></span> &#123;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p><strong>根据Path动画</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> keyFrameAnimation = <span class="type">CAKeyframeAnimation</span>(keyPath: <span class="string">"position"</span>)</span><br><span class="line">keyFrameAnimation.path = <span class="type">UIBezierPath</span>(arcCenter: <span class="type">CGPoint</span> (x: layer.bounds.width / <span class="number">2</span>, y: layer.bounds.width / <span class="number">2</span>), radius: <span class="number">20</span>, startAngle: <span class="number">0</span>, endAngle: .pi * <span class="number">2</span>, clockwise: <span class="literal">false</span>).cgPath</span><br><span class="line">keyFrameAnimation.timingFunction = <span class="type">CAMediaTimingFunction</span> (name: kCAMediaTimingFunctionEaseInEaseOut)</span><br><span class="line"><span class="comment">//  设置动画执行完毕之后不删除动画</span></span><br><span class="line">keyFrameAnimation.isRemovedOnCompletion = <span class="literal">false</span>    <span class="comment">//这两行的作用是执行完动画不反弹</span></span><br><span class="line"><span class="comment">//  设置保存动画的最新状态</span></span><br><span class="line">keyFrameAnimation.fillMode = kCAFillModeForwards</span><br><span class="line">        </span><br><span class="line">layer.add(keyFrameAnimation, forKey: <span class="literal">nil</span>)</span><br></pre></td></tr></table></figure>
<p><strong>删除动画</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">layer.add(keyFrameAnimation, forKey: <span class="string">"Key"</span>)</span><br><span class="line">layer.removeAnimation(forKey: <span class="string">"Key"</span>)	<span class="comment">//回到原来的位置</span></span><br></pre></td></tr></table></figure>
<h4 id="CoreAnimation-CATransition-专场动画"><a href="#CoreAnimation-CATransition-专场动画" class="headerlink" title="CoreAnimation-CATransition 专场动画"></a>CoreAnimation-CATransition 专场动画</h4><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">touchesBegan</span><span class="params">(<span class="number">_</span> touches: Set&lt;UITouch&gt;, with event: UIEvent?)</span></span> &#123;</span><br><span class="line"><span class="keyword">let</span> animation = <span class="type">CATransition</span>()</span><br><span class="line">animation.timingFunction = <span class="type">CAMediaTimingFunction</span> (name: kCAMediaTimingFunctionEaseInEaseOut)</span><br><span class="line">animation.duration = <span class="number">2</span></span><br><span class="line">animation.type = <span class="string">"rippleEffect"</span>     <span class="comment">// 动画过度类型</span></span><br><span class="line">animation.subtype = kCATransitionFromLeft   <span class="comment">//  动画过度方向 (有些动画不支持)</span></span><br><span class="line">animation.startProgress = <span class="number">0.0</span>   <span class="comment">//动画起点(在整体动画的百分比)</span></span><br><span class="line">animation.endProgress = <span class="number">1</span>   <span class="comment">//动画终点</span></span><br><span class="line">        </span><br><span class="line"><span class="keyword">let</span> pushVC = <span class="type">PushVC</span>()</span><br><span class="line"><span class="keyword">self</span>.navigationController?.pushViewController(pushVC, animated: <span class="literal">true</span>)</span><br><span class="line">navigationController?.view.layer.add(animation, forKey: <span class="literal">nil</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    animation.type = kCATransitionFade;  </span><br><span class="line">      </span><br><span class="line">    animation.type = kCATransitionPush;  </span><br><span class="line">      </span><br><span class="line">    animation.type = kCATransitionReveal;  </span><br><span class="line">      </span><br><span class="line">    animation.type = kCATransitionMoveIn;  </span><br><span class="line">      </span><br><span class="line">    animation.type = @<span class="string">"cube"</span>;  </span><br><span class="line"></span><br><span class="line">	animation.type = @<span class="string">"code"</span>;	<span class="comment">//淡入淡出 </span></span><br><span class="line">      </span><br><span class="line">    animation.type = @<span class="string">"suckEffect"</span>;  </span><br><span class="line">      </span><br><span class="line">    <span class="comment">// 页面旋转  </span></span><br><span class="line">    animation.type = @<span class="string">"oglFlip"</span>;  </span><br><span class="line">      </span><br><span class="line">    <span class="comment">//水波纹  </span></span><br><span class="line">    animation.type = @<span class="string">"rippleEffect"</span>;  </span><br><span class="line">      </span><br><span class="line">    animation.type = @<span class="string">"pageCurl"</span>;  </span><br><span class="line">      </span><br><span class="line">    animation.type = @<span class="string">"pageUnCurl"</span>;  </span><br><span class="line">      </span><br><span class="line">    animation.type = @<span class="string">"cameraIrisHollowOpen"</span>;  </span><br><span class="line">      </span><br><span class="line">    animation.type = @<span class="string">"cameraIrisHollowClose"</span>;</span><br></pre></td></tr></table></figure>
<h4 id="CoreAnimation-GroupAnimation"><a href="#CoreAnimation-GroupAnimation" class="headerlink" title="CoreAnimation-GroupAnimation"></a>CoreAnimation-GroupAnimation</h4><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> animationY = <span class="type">CABasicAnimation</span> (keyPath: <span class="string">"transform.translation.y"</span>)</span><br><span class="line">animationY.toValue = <span class="number">300</span></span><br><span class="line"><span class="keyword">let</span> animationScale = <span class="type">CABasicAnimation</span> (keyPath: <span class="string">"transform.scale"</span>)</span><br><span class="line">animationScale.toValue = <span class="number">0.0</span></span><br><span class="line"><span class="keyword">let</span> animationRotate = <span class="type">CABasicAnimation</span> (keyPath: <span class="string">"transform.rotation"</span>)</span><br><span class="line">animationRotate.toValue = <span class="type">Double</span>.pi * <span class="number">2</span></span><br><span class="line">        </span><br><span class="line"><span class="keyword">let</span> groupAnimation = <span class="type">CAAnimationGroup</span>()</span><br><span class="line">groupAnimation.animations = [animationY,animationScale,animationRotate]</span><br><span class="line">groupAnimation.duration = <span class="number">2</span></span><br><span class="line">groupAnimation.fillMode = kCAFillModeForwards</span><br><span class="line">groupAnimation.isRemovedOnCompletion = <span class="literal">false</span></span><br><span class="line">layer.add(groupAnimation, forKey: <span class="literal">nil</span>)</span><br></pre></td></tr></table></figure>
<h4 id="UIView封装的CoreAnimation动画"><a href="#UIView封装的CoreAnimation动画" class="headerlink" title="UIView封装的CoreAnimation动画"></a>UIView封装的CoreAnimation动画</h4><p><strong>UIView封装的动画不会反弹,位置会变,而CoreAnimationLayer动画不会</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//	专场动画</span></span><br><span class="line">flipFromLeft</span><br><span class="line"></span><br><span class="line">flipFromRight</span><br><span class="line"></span><br><span class="line">curlUp</span><br><span class="line"></span><br><span class="line">curlDown</span><br><span class="line"><span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">touchesBegan</span><span class="params">(<span class="number">_</span> touches: Set&lt;UITouch&gt;, with event: UIEvent?)</span></span> &#123;</span><br><span class="line">        <span class="keyword">let</span> view = <span class="type">UIView</span> (frame: <span class="type">CGRect</span> (x: <span class="number">0</span>, y: <span class="number">0</span>, width: <span class="number">100</span>, height: <span class="number">100</span>))</span><br><span class="line">        <span class="type">UIView</span>.transition(with: view, duration: <span class="number">2.0</span>, options: [], animations: &#123;</span><br><span class="line">            <span class="type">UIView</span>.setAnimationTransition(.flipFromLeft, <span class="keyword">for</span>: view, cache: <span class="literal">true</span>)</span><br><span class="line">        &#125;) &#123; (finished) <span class="keyword">in</span></span><br><span class="line">            </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/07/11/Quartz2D/" rel="next" title="Quartz2D">
                <i class="fa fa-chevron-left"></i> Quartz2D
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/07/14/UIViewAnimation/" rel="prev" title="UIViewAnimation">
                UIViewAnimation <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/10.png"
                alt="Erhqe" />
            
              <p class="site-author-name" itemprop="name">Erhqe</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">70</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">tags</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#隐式动画"><span class="nav-number">1.</span> <span class="nav-text">隐式动画</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#Layer绘制"><span class="nav-number">1.1.</span> <span class="nav-text">Layer绘制</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#CoreAnimation-BasicAnimation"><span class="nav-number">2.</span> <span class="nav-text">CoreAnimation-BasicAnimation</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#CoreAnimation-KeyFrameAnimation"><span class="nav-number">3.</span> <span class="nav-text">CoreAnimation-KeyFrameAnimation</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#CoreAnimation-CATransition-专场动画"><span class="nav-number">4.</span> <span class="nav-text">CoreAnimation-CATransition 专场动画</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#CoreAnimation-GroupAnimation"><span class="nav-number">5.</span> <span class="nav-text">CoreAnimation-GroupAnimation</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#UIView封装的CoreAnimation动画"><span class="nav-number">6.</span> <span class="nav-text">UIView封装的CoreAnimation动画</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Erhqe</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'TiAY2J24ECwQH9uLYsoJpgIK-gzGzoHsz',
        appKey: 'Y9PiVrPXVmfNsv4Aa6cPsADG',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
